%function decompUnconditional 
clear all; close all;clc
outPath = 'C:\Users\g1mxo01\Documents\MATLAB\Alex';
outName = 'conditional';
modelPath = 'O:\PROJ_LIB\FOMC\MACRO\DSGE\models\CEFJ\BigARMAHP\Prior9 Gap OverIdent HP FL\02 03 12\2011.75 02 09 12';

cd(modelPath);
load workspace;
cd('O:\PROJ_LIB\LJM\exog');
%% 1. Define What the decomposition will be 

histStru.report={'GDP','Consumption','Investment','FFR','Hours','PCE Core'};

histStru.groupnames={'Forward Guidance','Residual'};
groupStruct.g1={'Factor 2'};

% Date to Begin and End 
startDate=2007.25;
endDate=2011.75; 

% Options for plots
plotResidual = false;
plotSameScale = true;

%% Calculate Constants and Scales

constantsReal
% Vector of Model Steady States

ssVec(1) = paramGDP; 
ssVec(2) = paramCONS;
ssVec(3) = paramINV;
ssVec(4) = paramFFR;
ssVec(5) = paramHours;
ssVec(6) = paramPCECore;

% Scale to Multiply by
scale = [4 4 4 4 1 4];

%% 2. Solve Model and Extract Names
% [GG, RR, CONS, eu, SDX, ZZ, initss, ssvec,~, ssNames,stateNames,shockNamesStru]...
%     =feval(funcmod,param,solveopt,addsol);

%% 3. Define nameStru, Structure with the Names 

shockNames=shockNamesStru.long; 
[Znames,ZposInStates]=extractObsNames(ZZ(:,:,1),stateNames);
nameStru.stateNames=stateNames;
nameStru.shockNames=shockNames;
nameStru.obsNames=Znames;
shockNames=shockNamesStru.long; 

%% 4. Define sampleStr, Structure with all the sample information 

sampleStru.sampleVec=sample; 
sampleStru=sampleAssignments(sampleStru);
addsol.tauVec=sampleStru.tauVec;
% [KFStru,logL]=feval(addsol.funcKfilter,param,funcmod,Y,trainvec,solveopt,addsol); 

%% 5. Define Key Variables
reportPos = cellposition(histStru.report,stanames);
startPos = find(sampleStru.sampleVec == startDate);
endPos   = find(sampleStru.sampleVec == endDate);
subSample = sample(startPos:endPos);

gsum = zb_extractgroups(groupStruct,shockNames,histStru.groupnames); 
nGroups = length(histStru.groupnames); 
nReport = length(reportPos);
nForc = length(subSample)-1;
nx = length(shockNames);
ns = length(stanames);

%% 6. Calculate Conditional Forecasts begining at histru.startDate

condForc = zeros(nForc,ns,nGroups);
condForcDecomp = zeros(nForc,ns,nGroups);
tauVec = addsol.tauVec(startPos+1:endPos);
jumpState = KFStru.smoothSt(startPos,:);

eta = zeros(nForc,nx);
condForcShocksOff = forecastLoop(GG, RR, ZZ, CC, tauVec, jumpState, eta, nForc);
condForcError = KFStru.smoothSt(startPos+1:endPos,:) - condForcShocksOff;

for m = 1:nGroups
    
    groupInx = gsum.(['n' num2str(m)]);
    eta = zeros(nForc, nx);
    
    eta(:,groupInx) = KFStru.innovations(startPos+1:endPos,groupInx);
    
    stforc = forecastLoop(GG, RR, ZZ, CC, tauVec, jumpState, eta, nForc);
    condForc(:,:,m) = stforc;
    condForcDecomp(:,:,m) = stforc - condForcShocksOff;
    
end

additiveCheck = sum(sum(abs(sum(condForcDecomp,3) - condForcError)));

if additiveCheck > 1E-5
    error('Decompositions do not sum to forecast error');
end

%% Make Plot of Fed Funds with No FG

% FFR series
ffrYInx = cellposition('FFR', Znames);
ffrSInx = cellposition('FFR',stanames);
ffrData = 4*Y(startPos:endPos,ffrYInx);
ffrNoFG = [ffrData(1); 4*(condForc(:,ffrSInx,2) + ssVec(4))];

% Plot Struct
plotMat = [ffrData ffrNoFG];
plotStruct = plotAssignDefaults([]);
subSample = sample(startPos:endPos);
plotStruct.xtickValues = subSample(1:4:end);
plotStruct.xtickLabels = sampleSS.sampleCell(startPos:4:endPos);
plotStruct.legend = {'Data', 'No Foreward Guidance'};
plotStruct.yLabel = 'Annualized Rate';

plot3sampSub(subSample, plotMat, 'Fed Funds without Foreward Guidance', 0, plotStruct);
hand=hline(0); 
set(hand,'Color',[1 0 0],'LineStyle','--'); 
%plots2PDF(gcf,'fedFunds')








